package com.yanyun.yczyy.api.repository.impl;

import com.yanyun.yczyy.api.entities.EnergyUsedView;
import com.yanyun.yczyy.api.entities.EnergyValue;
import com.yanyun.yczyy.api.repository.IEnergyManagerRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Repository
public class EnergyManagerRepository implements IEnergyManagerRepository {

    @Override
    public List<EnergyUsedView> locationAEnergyUsed() {
        try {
            List<EnergyValue> ret = yczyy.query(locationASql, m_Mapper);
            if (ret.isEmpty()) {
                return null;
            }
            return usedViews(ret);

        } catch (Exception e) {
            log.error("查询医研能耗使用失败：", e);
            return null;
        }
    }

    public List<EnergyUsedView> locationBEnergyUsed() {
        try {
            List<EnergyValue> ret = yczyy.query(locationBSql, m_Mapper);
            if (ret.isEmpty()) {
                return null;
            }
            return usedViews(ret);

        } catch (Exception e) {
            log.error("查询医研能耗使用失败：", e);
            return null;
        }
    }

    private List<EnergyUsedView> usedViews(List<EnergyValue> values) {
        List<EnergyUsedView> view = new ArrayList<>();
        Map<String, List<EnergyValue>> locationHourGroup = values.stream().filter(a -> a.getValue() > 0).collect(Collectors.groupingBy(EnergyValue::getLocation));

        locationHourGroup.entrySet().stream().filter(a -> a.getValue().size() > 1).forEach(kv -> {
            for (int i = 0; i < kv.getValue().size(); i++) {
                if (i == kv.getValue().size() - 1)
                    return;
                float used = kv.getValue().get(i + 1).getValue() - kv.getValue().get(i).getValue();
                if (used < 0)
                    return;
                EnergyUsedView item = new EnergyUsedView();
                item.setLocation(kv.getValue().get(i).getLocation());
                item.setHour(kv.getValue().get(i).getHour());
                item.setUsed(used);
                view.add(item);
            }
        });
        return view;
    }

    @Autowired
    JdbcTemplate yczyy;
    private final String locationASql = "select substring_index(resource_name,'_',1) location, DATE_FORMAT(io_time,'%Y-%m-%d %H:00:00') hour,max(cast(value as float)) value from tbl_io_energy where io_time >date_add(now(),interval -1 day) and resource_name like ('%正向有功电能') and resource_name like '医研%'\n" +
            "group by substring_index(resource_name,'_',1),DATE_FORMAT(io_time,'%Y-%m-%d %H:00:00')\n" +
            "order by substring_index(resource_name,'_',1),DATE_FORMAT(io_time,'%Y-%m-%d %H:00:00')";
    private final String locationBSql = "select substring_index(resource_name,'_',1) location, DATE_FORMAT(io_time,'%Y-%m-%d %H:00:00') hour,max(cast(value as float)) value from tbl_io_energy where io_time >date_add(now(),interval -1 day) and resource_name like ('%正向有功电能') and resource_name like '门诊%'\n" +
            "group by substring_index(resource_name,'_',1),DATE_FORMAT(io_time,'%Y-%m-%d %H:00:00')\n" +
            "order by substring_index(resource_name,'_',1),DATE_FORMAT(io_time,'%Y-%m-%d %H:00:00')";
    private final RowMapper<EnergyValue> m_Mapper = new BeanPropertyRowMapper<>(EnergyValue.class);
}
